package org.ediba.siri.web.rest;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.ediba.siri.core.Filter;
import org.ediba.siri.core.action.domain.ActionRequest;
import org.ediba.siri.core.action.service.ActionService;
import org.ediba.siri.core.publication.domain.Publication;
import org.ediba.siri.core.publication.service.PublicationService;
import org.ediba.siri.core.user.service.UserService;
import org.springframework.web.bind.annotation.ResponseBody;

public class ActionRequestController extends Controller {
	
	private ActionService actionService;
	public void setActionService(ActionService actionService) {this.actionService = actionService;}
	
	private PublicationService publicationService;
	public void setPublicationService(PublicationService publicationService) {this.publicationService = publicationService;}
	
	private UserService userService;
	public void setUserService(UserService userService) {this.userService = userService;}
	
	@ResponseBody
	public Map<String,Object> getHandler(HttpServletRequest request, HttpServletResponse response, ActionRequestCommand command){

		//List<ActionRequest> ar = this.actionService.pendingActionRequestCurrent();
		//List<ActionDownload> ad = this.actionService.pendingActionDownloadCurrent();
		
		Integer total = 0;
		if (command.getSort() == null)
			command.setSort("image.name");
		if (command.getDir() == null)
			command.setDir(Boolean.TRUE);

		List<ActionRequest> actionRequests = new ArrayList<ActionRequest>();
		
		if (command.getId() != null){//un actionRequest en particular
//			ActionRequest actionRequest = this.actionService.requestRetrieve(command.getId());
//			actionRequests.add(actionRequest);
//			total = actionRequest!=null?1:0;
		}else if (command.getIdUser() != null){//los action request de un usuario
			Map<Filter,Object> filter = new HashMap<Filter, Object>();
			filter.put(Filter.USER, command.getIdUser());
			actionRequests.addAll(this.actionService.requestRetrieve(filter, command.getSort(), command.getDir(), command.getStart(), command.getLimit()));
			total = this.actionService.requestRetrieveCount(filter);
		}else{ //Todos los action request paginados y ordenados
			List<ActionRequest> result = new ArrayList<ActionRequest>();
			if (command.getPending()){//ActionRequest pendientes de recibir respuesta
				result = this.actionService.requestRetrieve(command.getSort(), command.getDir(), command.getStart(), command.getLimit());
				total = this.actionService.requestRetrieveCount();
			}else{//ActionRequest pendientes de contestar
				List<Publication> publications = this.publicationService.retrieveByResponsible(this.userService.retrieveCurrent().getId());
				Map<Filter,Object> filter = new HashMap<Filter, Object>();
				if (publications.size()!=0){
					List<Integer> idsPublication = new ArrayList<Integer>();
					for (Publication publication : publications) {
						idsPublication.add(publication.getId());
					}
					filter.put(Filter.PUBLICATION, idsPublication);
				}//else filter.put(Filter.PUBLICATION, null);
				result = this.actionService.requestRetrieve(filter,command.getSort(), command.getDir(), command.getStart(), command.getLimit());
				total = this.actionService.requestRetrieveCount(filter);
			}
			actionRequests.addAll(result);
		}
		Map<String,Object> _result = new HashMap<String,Object>(4);
		_result.put("data", actionRequests);
		_result.put("total", total);
		_result.put("success", true);
		_result.put("message", "OK");
		return _result;
	}
	
	@ResponseBody
	public Map<String,Object> grantHandler(HttpServletRequest request, HttpServletResponse response, ActionRequestCommand command){
		Integer idRequest = command.getId();
		
		int _error = this.actionService.grantRequest(idRequest);
		
		Map<String,Object> _result = new HashMap<String,Object>(2);
		_result.put("success", _error==ActionService.ERROR_OK);
		_result.put("message", "OK");
		return _result;
	}
	
	@ResponseBody
	public Map<String,Object> rejectHandler(HttpServletRequest request, HttpServletResponse response, ActionRequestCommand command){
		Integer idRequest = command.getId();
		
		int _error = this.actionService.rejectRequest(idRequest);
		
		Map<String,Object> _result = new HashMap<String,Object>(2);
		_result.put("success", _error==ActionService.ERROR_OK);
		_result.put("message", "OK");
		return _result;
	}
}
